package com.dexilaonian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dexilaonian.R;
import com.dexilaonian.fragment.HomeFragment;
import com.dexilaonian.ui.BaseActivity;
import com.dexilaonian.utils.Constant;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class ChangePwdActivity extends BaseActivity {
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.base_topText)
    TextView baseTopText;
    @Bind(R.id.base_choose)
    TextView baseChoose;
    @Bind(R.id.tv_changePwd)
    TextView tvChangePwd;
    @Bind(R.id.rl_change)
    RelativeLayout rlChange;
    private String phone;
    private String find_url= Constant.base_url + "/verify/sentSmsVerify";
    @Override
    public int getLayoutID() {
        return R.layout.activity_change_pwd;
    }

    @Override
    public void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        baseTopText.setText("修改密码");
        Intent intent = getIntent();
        phone = intent.getStringExtra("PHONE");
        if (!TextUtils.isEmpty(phone)) {
            tvChangePwd.setText(phone);
        }
    }

    @OnClick({R.id.back, R.id.rl_change})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.rl_change:
                if (!TextUtils.isEmpty(phone)) {
                    sendPhone(phone);
                } else {
                    showToast("请先登录");
                }
                break;
        }
    }
    private void sendPhone(final String myphone2) {
        showProgressBar(true);
        OkHttpUtils.post().url(find_url).addParams("phone",myphone2).addParams("type","1").build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast(e.getMessage());
                showProgressBar(false);
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e("xuxux", response);
                showProgressBar(false);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int data = Integer.parseInt(jsonObject.optString("status"));
                    Log.e("data", response);
                    if (data == 34) {
                        startActivity(new Intent(ChangePwdActivity.this, LoginActivity.class));
                        return;
                    }
                    if (data == 7) {
                        showToast("单手机号短信发送时间间隔少于1分钟");
                        return;
                    }
                    if (data == 6) {
                        showToast("单手机号单日发送短信超10条");
                        return;
                    }
                    if (data == 0) {
                        showToast("发送成功，请注意查收短信");
                        Intent intent = new Intent(ChangePwdActivity.this, Register_smsActivity.class);
                        if (!TextUtils.isEmpty(phone)) {
                            intent.putExtra("Change1", phone);
                        }
                        startActivity(intent);
                    } else {
                        showToast("发送失败");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
